#coding:utf-8
'''
多变量非线性方程求解
'''

import scipy
from scipy import optimize
import numpy as np

from matplotlib import pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

'''
y - x^3 - 2*x^2 + 1 = 0
y + x^2 - 1 = 0
'''
def f(x):
    return [x[1] - x[0] ** 3 - 2 * x[0] ** 2 + 1
            , x[1] + x[0] ** 2 - 1
            ]
result = optimize.fsolve(f,[1,1])
print('result = ',result)


# 可视化求解过程
x = np.linspace(-3, 2, 5000)
y1 = x**3 + 2 * x**2 -1
y2 = -x**2 + 1

fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y1, 'b', lw=1.5, label=r'$y = x^3 + 2x^2 - 1$')
ax.plot(x, y2, 'g', lw=1.5, label=r'$y = -x^2 + 1$')

ax.legend(loc=0)
ax.set_xlabel(r'$x$', fontsize=18)

plt.show()